# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qgis
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.42.0
pkgrel=2
pkgdesc='Geographic Information System (GIS) that supports vector, raster & database formats (mingw-w64)'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://qgis.org/'
msys2_repository_url="https://github.com/qgis/QGIS"
msys2_references=(
  'archlinux: qgis'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-draco"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-opencl-icd"
         "${MINGW_PACKAGE_PREFIX}-pdal"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-python-gdal"
         "${MINGW_PACKAGE_PREFIX}-python-owslib"
         "${MINGW_PACKAGE_PREFIX}-python-pyqt6"
         "${MINGW_PACKAGE_PREFIX}-python-qscintilla-qt6"
         "${MINGW_PACKAGE_PREFIX}-qca-qt6"
         "${MINGW_PACKAGE_PREFIX}-qt6-3d"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt6-location"
         "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
         "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-qt6-tools"
         "${MINGW_PACKAGE_PREFIX}-qtkeychain-qt6"
         "${MINGW_PACKAGE_PREFIX}-qscintilla-qt6"
         "${MINGW_PACKAGE_PREFIX}-qwt-qt6"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-spatialindex"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-opencl-clhpp"
             "${MINGW_PACKAGE_PREFIX}-sip"
             "${MINGW_PACKAGE_PREFIX}-pyqt-builder"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-netcdf"
             "${MINGW_PACKAGE_PREFIX}-postgresql")
#optdepends=("${MINGW_PACKAGE_PREFIX}-netcdf: mdal plugin"
#            "${MINGW_PACKAGE_PREFIX}-postgresql: postgres plugins")
source=("https://qgis.org/downloads/${_realname}-$pkgver.tar.bz2"
        "001-fix-building-with-mingw-w64-clang.patch"
        "002-fix-settingstree-init-order-clang.patch"
        "004-fix-customwidgets-install.patch")
sha256sums=('04b743397ee2375a1a0521578131fc514752f84db2f86225a9551d1f87e704e8'
            '82d8839992f08a26ea7caf5e908e5f4111806df211ae5cec91e2b86e49b43e75'
            '47e6471b92b85d97e58b617da326c15622c01786c19eb7d4d5814483becb75e4'
            '216dc67ef707f2e918afb7d190c9ea75c11065dea0d23d11b94cab5d8463bac3')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    apply_patch_with_msg \
      001-fix-building-with-mingw-w64-clang.patch \
      002-fix-settingstree-init-order-clang.patch
  fi
  apply_patch_with_msg \
    004-fix-customwidgets-install.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-ignored-attributes"
  else
    CXXFLAGS+=" -Wno-attributes"
  fi
  CXXFLAGS+=" -Wno-deprecated-declarations"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_CXX_STANDARD=17 \
    "${_extra_config[@]}" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DQGIS_BIN_SUBDIR=bin \
    -DQGIS_DATA_SUBDIR=share \
    -DQGIS_LIBEXEC_SUBDIR=bin \
    -DQGIS_PLUGIN_SUBDIR=lib/qgis/plugins \
    -DQT_PLUGINS_DIR=share/qt6/plugins \
    -DPREFER_INTERNAL_LIBS=OFF \
    -DWITH_INTERNAL_POLY2TRI=ON \
    -DWITH_INTERNAL_MESHOPTIMIZER=ON \
    -DWITH_INTERNAL_NLOHMANN_JSON=OFF \
    -DBUILD_WITH_QT6=ON \
    -DWITH_3D=ON \
    -DWITH_BINDINGS=ON \
    -DWITH_CUSTOM_WIDGETS=ON \
    -DWITH_EPT=ON \
    -DWITH_PDAL=ON \
    -DWITH_POSTGRESQL=ON \
    -DWITH_QTWEBKIT=OFF \
    -DWITH_QUICK=ON \
    -DWITH_QWTPOLAR=ON \
    -DENABLE_TESTS=OFF \
    -DWITH_GRASS=OFF \
    ../${_realname}-$pkgver

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-$pkgver/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
